package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院设置表中的所有信息
    @ApiOperation("获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    //删除指定id的医院设置
    @ApiOperation("删除指定id的医院设置")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //条件查询带分页
    @ApiOperation("条件查询带分页")
    @PostMapping("/findPage/{pageNo}/{pageSize}")
    public Result findPage(@PathVariable Long pageNo,
                           @PathVariable Long pageSize,
                           @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(pageNo,pageSize);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        //收集参数
        String hosname = hospitalSetQueryVo.getHosname();  //医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();  //医院编号
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    //添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1可用   0不可用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        //使用MD5工具生成签名密钥
        String signKey = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(signKey);

        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //根据医院id获取医院设置
    @ApiOperation("根据医院id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet != null){
            return Result.ok(hospitalSet);
        }else{
            return Result.fail();
        }
    }

    //修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result deleteBatchByIds(@RequestBody List<Long> ids){
        boolean remove = hospitalSetService.removeByIds(ids);
        if(remove){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //医院设置的锁定与解锁
    @ApiOperation("医院设置的锁定与解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


    //发送签名密钥
    @ApiOperation("发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        System.out.println(hoscode);
        System.out.println(signKey);
        //TODO 发送信息
        return Result.ok();
    }
}
